/**
 * Created by chenshengli on 2015/7/9 0009.
 */


var startDate = "2015-8-18";
var endDate = "2015-8-28";

var url = location.search;
var payment = false;
if (url.indexOf("payment") > -1) {
    payment = true;
}

var showUsers = function () {
    $.ajax({
        type: "get",
        url: "/users/list",

        cache: false,
        success: function (users) {
            for (var i = 0; i < users.length; i++) {
                var user = users[i];
                $("#userSelect").append("<option value='" + user.uid + "'>" + user.name + "</option>");
            }
        }
    });
}

//showUsers();

var allhistory = {};

function analyseHistorys(orders) {
    allhistory = {};
    for (var i = 0; i < orders.length; i++) {
        var order = orders[i];
        var $user = $('#' + order.uid);
        if ($user.length == 0) {
            var paymentStr = "";
            if (payment) {
                paymentStr = '<input type="button" onclick="payOrderForDays(\'' + order.uid + '\')" value="付款"/>';
            }
            $('<div id="' + order.uid + '" class="history" onclick="toggleUser(\'' + order.uid + '\');"><div class="header"><img src="/images/show.png" ><span class="person">' + order.name + '</span>' +
                '&nbsp;&nbsp;<span id="paycount"></span>&nbsp;&nbsp;<span id="topaycount"></span>' + paymentStr + '</div></div>').appendTo($('#historys'));

            allhistory[order.uid] = [];
        }
        var userHistory = allhistory[order.uid];
        userHistory[userHistory.length] = order;
    }

    for (var user in allhistory) {
        var $user = $("<div id='list-" + user + "' style='display:none;'></div>");
        var userHistory = allhistory[user];
        var userToPayCount = 0; // 待付款总额
        var userPayCount = 0;// 已付款总额
        for (var i = 0; i < userHistory.length; i++) {
            var historyOrder = userHistory[i];
            var state = "";
            if (historyOrder.payment == 1) {
                state = "已付款";
                userPayCount += parseFloat(historyOrder.price);
            } else {
                userToPayCount += parseFloat(historyOrder.price);
                state = "待付款";
            }
            var $history = $("<div class='list'><span class='history-detail'>" + historyOrder.date +
                "</span><span class='history-detail'>" + historyOrder.price +
                "</span><span class='history-detail'>" + historyOrder.meal +
                "</span><span class='history-detail'>" + state +
                "</span></div>");
            $history.appendTo($user);
        }
        $user.appendTo($('#' + user));
        $('#' + user).find('#paycount').html("已付款：￥" + userPayCount.toFixed(1));
        $('#' + user).find('#topaycount').html("待付款：￥" + userToPayCount.toFixed(1));
    }
}
var query = function () {
    $.ajax({
        type: "get",
        url: "/historys/query",
        data: {start: startDate, end: endDate},
        cache: false,
        success: function (orders) {
            analyseHistorys(orders);
        }
    });
}


var toggleUser = function (user) {
    var state = $('#list-' + user).css("display");
    if ("block" == state) {
        $('#' + user).find("img").attr("src", "/images/show.png");
        $('#list-' + user).hide();
    } else {
        $('#' + user).find("img").attr("src", "/images/hide.png");
        $('#list-' + user).show();
    }
}


query();

$(document).ready(function () {
    $('#dateRange').html(startDate + "至" + endDate);
})


var payOrderForDays = function (uid) {
    var userHistory = allhistory[uid];
    for (var i = 0; i < userHistory.length; i++) {
        var order = userHistory[i];
        $.ajax({
            type: "get",
            url: "/orders/payOrder?date=" + order.date + "&orderId=" + order._id,
            cache: false,
            async: false,
            success: function (result) {
                if (result.status == "ok") {
                } else {
                    alert("付款失败");
                }
            }
        });
    }
    alert("付款成功！");
    return false;
}